package io.renren.modules.norminfo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.renren.modules.alarmonline.service.WebSocketService;
import io.renren.modules.welderinfo.service.impl.WelderinfoServiceImpl;
import io.renren.modules.weldingmachineinfo.entity.WeldingmachineinfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.norminfo.dao.NorminfoDao;
import io.renren.modules.norminfo.entity.NorminfoEntity;
import io.renren.modules.norminfo.service.NorminfoService;
import org.springframework.web.client.RestTemplate;


@Service("norminfoService")
public class NorminfoServiceImpl extends ServiceImpl<NorminfoDao, NorminfoEntity> implements NorminfoService {

    @Autowired
    NorminfoDao norminfoDao;

    @Autowired
    WebSocketService webSocketService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<NorminfoEntity> page = this.page(
                new Query<NorminfoEntity>().getPage(params),
                new QueryWrapper<NorminfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<NorminfoEntity> getnorminfo(String normId, String normDescribr) {
        webSocketService.sendMessage(JSONObject.toJSONString(norminfoDao.getnorminfo(normId,normDescribr)));
        return norminfoDao.getnorminfo(normId,normDescribr);
    }

    //1.获取token
    public Object getToken() throws JsonProcessingException {
        RestTemplate restTemplate = new RestTemplate();

        // 获取token
//        UsersServiceImpl.ReHeader reHeader = new UsersServiceImpl.ReHeader();
        WelderinfoServiceImpl.ReHeader reHeader = new WelderinfoServiceImpl.ReHeader();
        reHeader.setKey("cmhi2023");
        String url = "http://10.142.3.129:8080/getOpenApiToken";
        String result = restTemplate.postForObject(url,reHeader, String.class);
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(result);
        String token = String.valueOf(jsonNode.get("data").get("token")).replace("\"","");
        return token.toString();
    }

    //2.获取焊机信息
//获取焊工信息
//    @Async("screenTaskExecutor")
//    @Scheduled(cron = "0/5 * * * * ?")
    public String getWmInfo() throws InterruptedException, JsonProcessingException, URISyntaxException {
        String url = "http://10.142.3.129:8080/system/technology/list";
        String token=getToken().toString();
        ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        RestTemplate restTemplate = new RestTemplate(requestFactory);

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setBearerAuth(token);
        RequestEntity<Void> requestEntity = new RequestEntity<>(httpHeaders, HttpMethod.GET,new URI(url));
        ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class);
        String norminfo = responseEntity.getBody();
//        System.out.println(weldmachineinfo);
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(norminfo);
            JsonNode rowsNode = jsonNode.get("rows");
            if(!jsonNode.isArray()){
                for (JsonNode rowNode:rowsNode) {
                    String normid = rowNode.get("technologyCode").asText();
                    String normdescribr = rowNode.get("technologyName").asText();
                    String curcentral = rowNode.get("stdPa").asText();
                    String curdown = rowNode.get("minWpa").asText();
                    String curup = rowNode.get("maxWpa").asText();
                    String volcentral = rowNode.get("stdPv").asText();
                    String voldown = rowNode.get("minWpv").asText();
                    String volup = rowNode.get("maxWpv").asText();
//
                    System.out.println("id:"+normid);
                    System.out.println("描述:"+normdescribr);
                    System.out.println("curcen:"+curcentral);
                    System.out.println("curdown:"+curdown);
                    System.out.println("curup:"+curup);
                    System.out.println("volcen:"+volcentral);
                    System.out.println("voldown:"+voldown);
                    System.out.println("volup:"+volup);
//                    saveMysql(we);
                    System.out.println("------------------");
                    NorminfoEntity norminfoEntity = new NorminfoEntity();
                    norminfoEntity.setNormid(normid);
                    norminfoEntity.setNormdescribr(normdescribr);
                    norminfoEntity.setCurcentral(curcentral);
                    norminfoEntity.setCurup(curup);
                    norminfoEntity.setCurdown(curdown);
                    norminfoEntity.setVolcentral(volcentral);
                    norminfoEntity.setVoldown(voldown);
                    norminfoEntity.setVolup(volup);
                    saveMysql(norminfoEntity);
                }
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return responseEntity.toString();
    }

    public void saveMysql(NorminfoEntity norminfoEntity){
        String normid = norminfoEntity.getNormid();
        int count = norminfoDao.countByNormId(normid);
        if(count>0){
            System.out.println("已经存在");
        } else {
            norminfoDao.addNorm(norminfoEntity);
        }
        System.out.println("save");
//        log.info("save");
    }
    //3.将焊机信息存入数据库


}